﻿using System;

using Microsoft.ServiceBus.Messaging;

namespace Eldert.IoT.Windows.ShipsEventProcessor
{
    class ShipsEventProcessor
    {
        private static EventProcessorHost _eventProcessorHost;

        static void Main()
        {
            // Create connection string to storage which will hold info on which client is handling which partition 
            var storageConnectionString = $"DefaultEndpointsProtocol=https;AccountName={Settings.Read("StorageAccount")};AccountKey={Settings.Read("StorageAccountKey")}";

            // Create a unique identifier, which will be used to determine which partitions this processor is handling
            //var eventProcessorHostName = Environment.MachineName;
            var eventProcessorHostName = Guid.NewGuid().ToString();

            // Create the processor
            _eventProcessorHost = new EventProcessorHost(eventProcessorHostName, Settings.Read("FieldHubEventHub"), Settings.Read("FieldHubEventHubConsumerGroup") /* EventHubConsumerGroup.DefaultGroupName */, 
                Settings.Read("FieldHubEventHubConnectionString"), storageConnectionString);

            // Register the processor, at this point one or more partitions will be registered with the client, and it will start processing
            _eventProcessorHost.RegisterEventProcessorAsync<SimpleEventProcessor>().Wait();

            try
            {
                Console.WriteLine("Receiving. Press any key to stop.");
                Console.ReadKey();
            }
            finally
            {
                _eventProcessorHost.UnregisterEventProcessorAsync().Wait();
            }
        }
    }
}
